/*
 * BrandStreetProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Brand;
import com.yidiandao.business.data.recyclertype.DoubleBrand;
import com.yidiandao.business.ui.brandabout.brandgoods.BrandGoodsActivity;
import com.yidiandao.business.utils.ImageLoader;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 品牌街 Provider
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 14:00
 */
public class BrandStreetProvider
        extends ItemViewProvider<DoubleBrand, BrandStreetProvider.ViewHolder> {

    private Activity mActivity;

    public BrandStreetProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {

        View rootView = inflater.inflate(R.layout.item_brand_street, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull DoubleBrand doubleBrand) {

        final Brand brand1 = doubleBrand.getLeftBrand();
        final Brand brand2 = doubleBrand.getRightBrand();

        ImageLoader.loadImageWithRes(
                mActivity, holder.ivLogo1, brand1.getBrandLogo(), R.mipmap.ic_image_normal_2_66);
        holder.ivLogo1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BrandGoodsActivity.actionStart(
                                mActivity, brand1.getBrandName(), brand1.getBrandId(), null);
                    }
                });

        if (brand2 == null) {
            holder.rlytRoot2.setVisibility(View.INVISIBLE);
            return;
        }

        holder.rlytRoot2.setVisibility(View.VISIBLE);

        ImageLoader.loadImageWithRes(
                mActivity, holder.ivLogo2, brand2.getBrandLogo(), R.mipmap.ic_image_normal_2_66);

        holder.ivLogo2.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BrandGoodsActivity.actionStart(
                                mActivity, brand2.getBrandName(), brand2.getBrandId(), null);
                    }
                });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivLogo1;

        private ImageView ivLogo2;

        private RelativeLayout rlytRoot2;

        private ViewHolder(View itemView) {
            super(itemView);
            ivLogo1 = (ImageView) itemView.findViewById(R.id.iv_logo_1);
            ivLogo2 = (ImageView) itemView.findViewById(R.id.iv_logo_2);
            rlytRoot2 = (RelativeLayout) itemView.findViewById(R.id.rlyt_root_2);
        }
    }
}
